package cn.edu.hatu.bookshop.service;

import cn.edu.hatu.bookshop.dao.CustomerMapper;
import cn.edu.hatu.bookshop.entity.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerService {
    private CustomerMapper customerMapper;
    @Autowired
    public void setCustomerMapper(CustomerMapper customerMapper) {
        this.customerMapper = customerMapper;
    }

    public List<Customer> getAll(){
        List<Customer> customers = customerMapper.getAll();
        return customers;
    }

    public Customer findById(int customerId){
        Customer customer = customerMapper.findById(customerId);
        return customer;
    }

    public List<Customer> findByName(String customerName){
        List<Customer> customers = customerMapper.findByName(customerName);
        return customers;
    }
    public void delById(int customerId){
        customerMapper.delById(customerId);
    }
    public void addCustomer(Customer customer){
        customerMapper.addCustomer(customer);
    }
    public void modifyCustomer(Customer customer){
        customerMapper.updateCustomer(customer);
    }
    public Customer loginCheck(String name ,String password){
        List<Customer> customers = customerMapper.getAll();
        int customerId = 0;
        Customer customer = new Customer();
        customer.setCustomerId(customerId);
        for (Customer c:customers
             ) {
            if(c.getName().equals(name)&&c.getPassword().equals(password)){
                customerId = c.getCustomerId();
            }
        }
        if(customerId!=0){
            customer = customerMapper.findById(customerId);
        }

        return customer;
    }
    public void changePassword(String password,int customerId){
        customerMapper.changePassword(password,customerId);
    }
}
